//
//  updateUserInfoViewController.swift
//  万才信息App
//
//  Created by 陌上互联  on 16/7/17.
//  Copyright © 2016年 万才信息. All rights reserved.
//

import UIKit

class updateUserInfoViewController: UITableViewController {

    
    @IBOutlet weak var tvName: UITextField!
    @IBOutlet weak var sgSex: UISegmentedControl!
    @IBOutlet weak var sgMarriage: UISegmentedControl!
    @IBOutlet weak var dpBirth: UIDatePicker!
    @IBOutlet weak var tvPhone: UITextField!
    @IBOutlet weak var tvEmail: UITextField!
    
    
    @IBOutlet weak var pkDegree: UIPickerView!
    @IBOutlet weak var pkWorkYear: UIPickerView!
    @IBOutlet weak var pkCity: UIPickerView!
    
    
    let degreeSource=degreePickerDataSourceAndDelegate()
    let workyearSource=workyeatDataSourceAndDelegate()
    let citySource=cityPickerDataSourceAndDelegate()
    
    
//    加载视图时
    override func viewDidLoad() {
        super.viewDidLoad()
        
        loadPicker()
        loadUserData()
    }
    
//    按下提交的时候
    @IBAction func pressSubmit(sender: AnyObject) {
        
        let degree=degreeSource.degreeData[pkDegree.selectedRowInComponent(0)].0
        let workyear=workyearSource.workyearData[pkWorkYear.selectedRowInComponent(0)].0
        let city=citySource.cityData[pkCity.selectedRowInComponent(1)].0
        let username=tvName.text
        let sex=sgSex.selectedSegmentIndex+1
        let marriage=sgMarriage.selectedSegmentIndex+1
        let birthday=dpBirth.date
        let phone=tvPhone.text
        let email=tvEmail.text
        
        waiting.show(self)
        basicInfo.changeUserInfoByID(userUtil.getCurrUserId(), userName: username!, gender: sex, marriage: marriage, birthday: birthday, degree: degree, workyear: workyear, mobile: phone!, email: email!, location: city, successCallback: { (data) in
//            更改信息ok
            self.waiting.stop()
//            退出
            self.navigationController?.popViewControllerAnimated(true)
            }) { (error) in
                self.waiting.stop()
                warningAlertUtil(title: "错误", msg: error).show(self)
        }
    }
    
    
//    自己的方法：
    
//    加载dataPicker：
    private func loadPicker(){
        pkDegree.dataSource=degreeSource
        pkDegree.delegate=degreeSource
        
        pkCity.dataSource=citySource
        pkCity.delegate=citySource
        
        pkWorkYear.dataSource=workyearSource
        pkWorkYear.delegate=workyearSource
    }
    
    private let waiting=waitingAlertUtil(msg: "请稍后..")
    private let CURR_USER_ID=userUtil.getCurrUserId()
    
    private func loadUserData(){
        waiting.show(self)
        basicInfo.getUserInfoByID(CURR_USER_ID, successCallback: { (data) in
            //            匹配信息
            self.waiting.stop()
            
            if(data == nil){
                //                用户没有填写信息
            }else{
                //                用户填写了信息
                self.tvName.text=data["UserName"].string
                self.sgSex.selectedSegmentIndex=Int(data["Gender"].string!)!-1
                self.sgMarriage.selectedSegmentIndex = Int(data["Marrige"].string!)!-1
                self.tvPhone.text=data["Mobile"].string
                self.tvEmail.text=data["Email"].string
            }
        }) { (error) in
            self.waiting.stop()
            warningAlertUtil(title: "错误", msg: "获取用户个人信息失败").show(self)
        }

    }
    
    

    
}
